package Cantella::DJabberd::Store::ResultSet::User;

use base 'DBIx::Class::ResultSet';
use metaclass 'Reaction::Meta::Class';
use Reaction::Class;

use namespace::clean -except => [ 'meta' ];

sub login_user {
  my ($self, %args) = @_;
  if(my $user = $self->find( { username => $args{username} } )){
    if ($user->check_password($args{password})) {
      $self->_set_current_user($user);
      return 1;
    }
  }
  return 0;
}

sub logout_user {
  shift->_clear_current_user;
}

my $meta = __PACKAGE__->meta;
foreach my $meth (qw(current_user has_current_user _set_current_user _clear_current_user)){
  $meta->add_method($meth, sub { shift->result_source->schema->$meth(@_); });
};

__PACKAGE__->meta->make_immutable(inline_constructor => 0);

1;
